app.controller('forgetPwdPhoneCtrl',['$scope','$state','$cookies','toaster','forgetPasswordService','$interval',
    function($scope,$state,$cookies,toaster,forgetPasswordService,$interval){
        $scope.pwdPhone={};
        $scope.passwordPhoneForm = function(){
            var passwordToken = "mobilePhone="+$scope.pwdPhone.phone+"&code="+$scope.pwdPhone.code+"&password="+$scope.pwdPhone.password+"&method=0";
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            $scope.pwdPhone.passwordToken = encodeURIComponent(encrypt.encrypt(passwordToken));
            forgetPasswordService.forgetPasswordPhone($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $state.go('access.login');
                        break;
                    case 647:
                        alert("验证码输入错误");
                        break;
                    case 608:
                        alert("验证码已过期,请重新获取");
                        break;
                    case 650:
                        alert("未找到该手机");
                        break;
                    default :
                        alert("发生未知错误！请联系管理员");
                        break
                }
            });
        }
        $scope.description = "获取验证码";
        $scope.getCode = function() {
            if(!angular.isUndefined($scope.pwdPhone.phone)) {
                $scope.canClick = false;
                var second = 59;
                var timerHandler;
                forgetPasswordService.getMobilePhoneCode($scope).then(function (result) {
                    switch (result.status) {
                        case 200:
                            alert("验证码发送成功,请注意查收");
                            timerHandler = $interval(function () {
                                if (second <= 0) {
                                    $interval.cancel(timerHandler);    //当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                                    second = 59;
                                    $scope.description = "获取验证码";
                                    $scope.canClick = false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                                } else {
                                    $scope.description = second + "s后重发";
                                    second--;
                                    $scope.canClick = true;
                                }
                            }, 1000)   //每一秒执行一次$interval定时器方法
                            break;
                        case 650:
                            alert("未找到该手机");
                            break;
                        case 645:
                            alert("操作太频繁,无法发送手机验证码");
                            break;
                        default :
                            alert("发生未知错误！请联系管理员");
                            break
                    }
                });
            }
        }
    }]);


app.controller('forgetPwdEmailCtrl',['$scope','$state','$cookies','toaster','forgetPasswordService','$interval',
    function($scope,$state,$cookies,toaster,forgetPasswordService,$interval){
        $scope.pwdEmail={};
        $scope.passwordEmailForm = function(){
            var passwordToken = "email="+$scope.pwdEmail.email+"&code="+$scope.pwdEmail.code+"&password="+$scope.pwdEmail.password+"&method=0";
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            $scope.pwdEmail.passwordToken = encodeURIComponent(encrypt.encrypt(passwordToken));
            forgetPasswordService.forgetPasswordEmail($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $state.go('access.login');
                        break;
                    case 647:
                        alert("验证码输入错误");
                        break;
                    case 608:
                        alert("验证码已过期,请重新获取");
                        break;
                    case 651:
                        alert("未找到该邮箱");
                        break;
                    default :
                        alert("发生未知错误！请联系管理员");
                        break
                }
            });
        }
        $scope.description = "获取验证码";
        $scope.getCodeEmail = function() {
            if(!angular.isUndefined($scope.pwdEmail.email)) {
                $scope.canClick = false;
                var second = 59;
                var timerHandler;
                forgetPasswordService.getEmailCode($scope).then(function (result) {
                    switch (result.status) {
                        case 200:
                            alert("验证码发送成功,请注意查收");
                            timerHandler = $interval(function () {
                                if (second <= 0) {
                                    $interval.cancel(timerHandler);    //当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                                    second = 59;
                                    $scope.description = "获取验证码";
                                    $scope.canClick = false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                                } else {
                                    $scope.description = second + "s后重发";
                                    second--;
                                    $scope.canClick = true;
                                }
                            }, 1000)   //每一秒执行一次$interval定时器方法
                            break;
                        case 651:
                            alert("未找到该邮箱");
                            break;
                        default :
                            alert("发生未知错误！请联系管理员");
                            break
                    }
                });
            }
        }
    }]);